package com.jdrx.ocp.handlers;

import com.jdrx.ocp.beans.constans.BooleanEnum;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Boolean与Integer的handler(0:false,1:true)
 * @author : zhongsen
 * @date : 2019/02/21 14:35
 */
public class BooleanEnumTypeHandler implements TypeHandler<Boolean> {

    @Override
    public void setParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, BooleanEnum.getTypeByBoolean(parameter));
    }

    @Override
    public Boolean getResult(ResultSet rs, String columnName) throws SQLException {
        int i = rs.getInt(columnName);
        return  BooleanEnum.getValueByType(i);
    }

    @Override
    public Boolean getResult(ResultSet rs, int columnIndex) throws SQLException {
        int i = rs.getInt(columnIndex);
        return  BooleanEnum.getValueByType(i);
    }

    @Override
    public Boolean getResult(CallableStatement cs, int columnIndex) throws SQLException {
        int i = cs.getInt(columnIndex);
        return  BooleanEnum.getValueByType(i);
    }
}
